/**
 * create by pntrinh87@gmail.com
 * create date 29/11/2010
 */
TrGallery={
    countImg    : 0,
    
    fnow        : 0,
    
    imgnow      : 0,
    
    arrImg : new Array(),
    
    arrTitle: new Array(),
    
    ImgThumbs   : new Array(),
    
    frames      : new Array(),
    imgLOADED      : new Array(),
    countframe  : 0,
    
    divThumb    : 0,
    
    tmovet      : 0,
    
    dImgShow    : 0,
    prevS       : 0,
    nextS       : 0,
    dLoading    : 0,
    dCamera     : 0,
    
    addImg : function(arg)
    {
        this.arrImg[this.countImg]=arg.f;
        this.arrTitle[this.countImg]=arg.t;
        this.countImg++;
    },
    init : function(arg)
    {
        var _root    = this;
        arg.w        = 77*(arg.imginf/arg.rowt);
        this.arg     = arg;
        var divShow  = document.getElementById("PhotoShow");
        var divpn    = document.createElement("div");
        this.prevS   = document.createElement("div");
        this.nextS   = document.createElement("div");
        this.dImgShow= document.createElement("img");    
        this.dLoading= document.createElement("div");
        this.dCamera = document.createElement("div");
        this.cicon   = document.createElement("div");
        this.dicon   = document.createElement("div");
        this.cicon.className="cicon";
        this.dicon.className  = "dicon";
        this.dLoading.className="loading";      
        this.dCamera.className="camera";
        this.dCamera.style.display='none';
        divShow.className ='hide';      
        this.dImgShow.onselect=function(){return false};
        this.cicon.onmouseover=function()
        {
            _root.dicon.innerHTML=_root.arrTitle[_root.imgnow];
        };
        this.cicon.onmouseout=function()
        {
             _root.dicon.innerHTML='';
        };
        this.dImgShow.onmouseover=this.prevS.onmouseover=this.nextS.onmouseover=function()
        {
            divShow.className ='show';
        };
        this.dImgShow.onmouseout=function()
        {
            divShow.className ='hide';
        };
        this.prevS.onclick=function()
        {
            _root.imgnow--;
            if(_root.imgnow<0)
            {
                _root.imgnow=0;
            }
            _root.showImg();
        };
        this.nextS.onclick=function()
        {
            _root.imgnow++;
            if(_root.imgnow>=_root.countImg)
            {
                _root.imgnow=_root.countImg-1;
            }
            _root.showImg();
        };
        
        this.prevS.id="prevS";
        this.nextS.id="nextS";
        this.prevS.className = "prevS";
        this.nextS.className = "nextS";  
        divpn.className      = "PrNeS";
        divpn.style.width    = divShow.style.width;
        divpn.style.marginTop= (divShow.style.height).replace('px','')/2;
        divShow.appendChild(this.dLoading); 
        divShow.appendChild(this.cicon);   
        divShow.appendChild(this.dicon);   
        divpn.appendChild(this.prevS);
        divpn.appendChild(this.nextS);
        divShow.appendChild(divpn);
        divShow.appendChild(this.dImgShow);        
        this.divThumb=document.getElementById(arg.dt);
        this.divThumb.style.width = _root.arg.w + "px";
        this.countframe           = Math.ceil(this.countImg /_root.arg.imginf)-1;
        for(i=0;i<=this.countframe;i++)
        {
            this.frames[i]=document.createElement("div");
            this.frames[i].className="thumb4";
            this.frames[i].style.marginLeft= i*_root.arg.w + "px";
            this.frames[i].style.width     = _root.arg.w+4 +"px";
            for(j=0;j<_root.arg.imginf;j++)
            {
                var idd            = i*_root.arg.imginf+j;
                this.ImgThumbs[idd]=document.createElement("img");
                this.ImgThumbs[idd].className= "th1";
                if(this.arrImg[idd]!=undefined)
                {
                    this.ImgThumbs[idd].src=this.arrImg[idd];                    
                    this.ImgThumbs[idd].idd=idd;
                    this.ImgThumbs[idd].onclick=this.dCamera.onclick=function()
                    {
                        _root.imgnow=this.idd;
                        _root.showImg();   
                        if(arg.action)
                        {
                            eval(arg.action);
                        }
                    };
                    this.ImgThumbs[idd].onmouseover=function()
                    {
                        document.getElementById("GDescription").innerHTML=_root.arrTitle[this.idd];
                        _root.dCamera.idd=this.idd;
                        _root.dCamera.style.display='';
                        _root.dCamera.style.marginTop=Math.ceil((this.idd+1)/(_root.arg.imginf/_root.arg.rowt)-1)%(_root.arg.rowt)*52 + 'px';
                        var cln=((this.idd%(_root.arg.imginf/_root.arg.rowt))+1)+(_root.fnow*_root.arg.imginf/_root.arg.rowt)-1;
                        _root.dCamera.style.marginLeft=cln*73+((cln+1)*4)+"px";
                    };
                    _root.dCamera.onmouseover=function()
                    {
                        this.style.display='';
                    };
                    this.ImgThumbs[idd].onmouseout=_root.dCamera.onmouseout=function()
                    {
                        _root.dCamera.style.display='none';
                    }
                }else
                {
                    this.ImgThumbs[idd].src="/images/clear.gif";
                }                
                this.frames[i].appendChild(this.ImgThumbs[idd]);
            }
            this.divThumb.appendChild(this.frames[i]);
        }  
        this.divThumb.appendChild(this.dCamera);
        _root.showImg();
        this.goFrame(0);
    },
    goFrame : function(arg)
    {
        var _root=this;
        this.fnow += arg;
        if(this.fnow<0)
        {
            this.fnow=0;
        }
        else if(this.fnow>_root.countframe)
        {
            this.fnow   = _root.countframe;
        }
        if(this.fnow==0)
        {
            document.getElementById("Prev").className="PrevnAct";
        }
        else
        {
            document.getElementById("Prev").className="PrevAct";
        }
        if(this.fnow==_root.countframe || _root.countframe<1)
        {
            document.getElementById("Next").className="NextnAct";
        }else
        {
            document.getElementById("Next").className="NextAct";
        }
        var disc   = parseInt((this.frames[this.fnow].style.marginLeft).replace('px',''))+4; 
        clearInterval(this.tmovet);
        this.tmovet=setInterval(function(){_root.anami(_root.divThumb.scrollLeft,disc)},25);
        document.getElementById("ppage").innerHTML=(this.fnow+1)+"/"+(_root.countframe+1);
    },
    anami : function(last,disc)
    {
        var cc=((disc-last)/(this.arg.w/35));
        cc=(cc>0)? Math.ceil(cc) : Math.floor(cc);
        this.divThumb.scrollLeft+=cc;   
        if(this.divThumb.scrollLeft==disc)
        {
            clearInterval(this.tmovet);
        }
    },
    addClass : function(n,c)
    {
        this.removeClass(n,c);
        n.className = n.className ? n.className +' '+ c : c; 
    },
    removeClass : function(n,c)
    {
        c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
		return n.className = c != ' ' ? c : '';
    },
    showImg : function()
    {
        var _root   = this;
        this.dImgShow.src=this.arrImg[this.imgnow].replace("thumbs/","");        
        this.dImgShow.alt=this.dImgShow.title=(this.arrTitle[this.imgnow]!=undefined)? this.arrTitle[this.imgnow]: "not found";  
        for(i=0;i<this.countImg;i++)
        {
            if(i==this.imgnow)
            {
                this.addClass(this.ImgThumbs[this.imgnow],"selected");
            }else
            {
                this.removeClass(this.ImgThumbs[i],"selected");
            }
        }
        this.prevS.className=(this.imgnow>0)? "prevSAct" : "prevSnAct";
        this.nextS.className=(this.imgnow<this.countImg-1)? "nextSAct" : "nextSnAct";
        var fn= Math.ceil((this.imgnow+1) /_root.arg.imginf)-1;
        if(fn!=this.fnow)
        {
            this.goFrame(fn-this.fnow);
        }
        if(_root.imgLOADED[_root.imgnow]!=true)
        {
            _root.dLoading.st=setTimeout(function(){_root.dLoading.style.display='';},300);
        }        
        this.dImgShow.onload=function()
        {
            clearTimeout(_root.dLoading.st); 
            _root.dLoading.style.display='none';   
            _root.imgLOADED[_root.imgnow]=true;             
        };
    }
}
